package com.check.operation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.R;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.RequestUse;
import com.check.operation.model.Surgictz;
import com.check.operation.model.User;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SstzAdapter extends BaseAdapter implements OnClickListener{
	private LayoutInflater layoutInflater;
	private List<Surgictz> cws;
	private int index=-1;
	private User user;
	PlaySoundPool playSoundPool;

	public SstzAdapter(LayoutInflater layoutInflater,User user) {
		super();
		this.layoutInflater = layoutInflater;
		this.user=user;
		playSoundPool=new PlaySoundPool(layoutInflater.getContext());
		playSoundPool.loadSfx(R.raw.ok, 1);
	    playSoundPool.loadSfx(R.raw.error, 2);
	}

	@Override
	public int getCount() {
		return cws.size();
	}

	@Override
	public Surgictz getItem(int position) {
		return cws.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		try {
			Surgictz operation=getItem(position);
			ViewHolder holder=null;
			
			if(view==null){
				view=layoutInflater.inflate(R.layout.sstzitem, null);
				holder=new ViewHolder();
				holder.zyh=(TextView) view.findViewById(R.id.zyh);
				holder.xm=(TextView) view.findViewById(R.id.xm);
				holder.ks=(TextView) view.findViewById(R.id.ks);
				holder.ssmc=(TextView) view.findViewById(R.id.ssmc);
				holder.zdys=(TextView) view.findViewById(R.id.zdys);
				holder.ssbw=(TextView) view.findViewById(R.id.ssbw);
				holder.ssfj=(TextView) view.findViewById(R.id.ssfj);
				holder.sszt=(TextView) view.findViewById(R.id.sszt);
				holder.tzys=(Button) view.findViewById(R.id.tzys);
				holder.tzhs=(Button) view.findViewById(R.id.tzhs);
				
				holder.tzys.setOnClickListener(this);
				holder.tzhs.setOnClickListener(this);
				
				holder.sstab=(TableLayout) view.findViewById(R.id.sstab);
				
				view.setTag(holder);
			}else{
				holder=(ViewHolder) view.getTag();
			}
			
			if(index==position){
				holder.sstab.setBackgroundResource(R.color.黄色);
			}else{
				holder.sstab.setBackgroundResource(R.color.白色);
			}
			
			holder.operation=operation;
			holder.zyh.setText(operation.inpatient_no.trim());
			String xm = operation.inpatient_name.trim().replaceAll("[#$&*]", "");
			holder.xm.setText(xm);
			holder.ks.setText(operation.dept_name);
			holder.ssmc.setText(operation.surgic_name);
			holder.zdys.setText(operation.doctor_name);
			holder.ssbw.setText(operation.op_position);
			holder.ssfj.setText(operation.op_room);
			holder.sszt.setText(operation.status_text);
			holder.tzys.setTag(holder);;
			holder.tzhs.setTag(holder);;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	static class ViewHolder{
		TextView zyh;
		TextView xm;
		TextView ks;
		TextView ssmc;
		TextView zdys;
		TextView ssbw;
		TextView ssfj;
		TextView sszt;
		Button tzys;
		Button tzhs;
		TableLayout sstab;
		Surgictz operation;
	}

	public List<Surgictz> getCws() {
		return cws;
	}

	public void setCws(List<Surgictz> cws) {
		this.cws = cws;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	@Override
	public void onClick(View v) {
		final Button butt=(Button) v;
		butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.紫色));
		ViewHolder holder= (ViewHolder) butt.getTag();
		Surgictz operation =holder.operation;
		
	    String action="notification/notify";
 	    final RequestUse requestUse=new RequestUse();
 	    requestUse.user=this.user;
 	    Map<String,Object> params1=new HashMap<String, Object>();
 	    params1.put("surgic_no", operation.surgic_no);
 	    int type=0;
	    if(v.getId()==R.id.tzys){
	    	  type=2;
	    	  params1.put("type",String.valueOf(2));
		}else if(v.getId()==R.id.tzhs){
			  type=1;
			  params1.put("type", String.valueOf(1));
		}
 	   requestUse.request=params1;
 	   String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
 	   if(rs.indexOf("error")>0){
 		  playSoundPool.play(2, 0);
 		   Message me=JSONObject.parseObject(rs, Message.class);
 		   Toast.makeText(layoutInflater.getContext(), me.text, 800).show();
 	   }else{
 		  MessagRe<Surgictz> mereturn=JSONObject.parseObject(rs, new TypeReference<MessagRe<Surgictz>>(){});
 		  if("success".equals(mereturn.message.code)){
 			 playSoundPool.play(1, 0);
 			  if(type==1){
 				 holder.sszt.setText("已通知护士");
 			  }else if(type==2){
 				 holder.sszt.setText("已通知医生");
 			  }
 			 
 		  }
 		  Toast.makeText(layoutInflater.getContext(), mereturn.message.text, 800).show();
 	   }
 	  
	
	}

}
